using System.Collections.Generic;
using OracleInternal.Common;

namespace OracleInternal.ConnectionPool
{
	internal class ConStringPool : Pooler<int, List<ConnectionString>>
	{
		private object m_sync;

		public ConStringPool(int maxCacheSize)
			: base(maxCacheSize, 1, PoolerItemOwnership.Shared)
		{
			m_sync = new object();
		}

		public bool Remove(ConnectionString val)
		{
			List<ConnectionString> list = null;
			if (ContainsKey(val.m_key))
			{
				list = Get(val.m_key);
				if (list != null)
				{
					lock (list)
					{
						val.m_bDeadObject = true;
						return list.Remove(val);
					}
				}
			}
			if (list != null && list.Count == 0)
			{
				lock (m_sync)
				{
					if (list.Count == 0)
					{
						return Remove(val.m_key);
					}
				}
			}
			return false;
		}

		public void Put(ref ConnectionString val, bool bReplace = false)
		{
			if (val.m_bDeadObject || (val.m_bPooled && !bReplace))
			{
				return;
			}
			List<ConnectionString> list = null;
			bool flag = false;
			if (!ContainsKey(val.m_key))
			{
				lock (m_sync)
				{
					if (!ContainsKey(val.m_key))
					{
						if (val.m_bDeadObject || (val.m_bPooled && !bReplace))
						{
							return;
						}
						list = new List<ConnectionString>(4);
						val.m_bPooled = true;
						list.Add(val);
						base.Put(val.m_key, list);
						flag = true;
					}
				}
			}
			if (flag)
			{
				return;
			}
			bool flag2 = false;
			lock (m_sync)
			{
				if (val.m_bDeadObject || (val.m_bPooled && !bReplace))
				{
					return;
				}
				list = Get(val.m_key);
				if (list == null)
				{
					return;
				}
				lock (list)
				{
					for (int i = 0; i < list.Count; i++)
					{
						if (flag2)
						{
							break;
						}
						ConnectionString connectionString = list[i];
						if (!(connectionString.m_compString == val.m_compString) || !(connectionString.m_osUserName == val.m_osUserName))
						{
							continue;
						}
						bool flag3 = false;
						bool flag4 = false;
						if (val.m_orclCredential != null && connectionString.m_orclCredential != null)
						{
							flag3 = val.m_orclCredential.Equals(connectionString.m_orclCredential);
						}
						else
						{
							flag4 = ConnectionString.SecureStringsEqual(connectionString.m_securedPassword, val.m_securedPassword) && ConnectionString.SecureStringsEqual(connectionString.m_securedProxyPassword, val.m_securedProxyPassword);
						}
						if (!(flag3 || flag4))
						{
							continue;
						}
						flag2 = true;
						if (bReplace)
						{
							if (!connectionString.Equals(val))
							{
								connectionString.m_bPooled = false;
								list[i] = val;
								val.m_bPooled = true;
							}
						}
						else
						{
							val = connectionString;
						}
						break;
					}
					if (!flag2)
					{
						val.m_bPooled = true;
						list.Add(val);
					}
				}
			}
		}

		public void Put(ConnectionString val)
		{
			if (val.m_bDeadObject || val.m_bPooled)
			{
				return;
			}
			List<ConnectionString> list = null;
			bool flag = false;
			if (!ContainsKey(val.m_key))
			{
				lock (m_sync)
				{
					if (!ContainsKey(val.m_key))
					{
						if (val.m_bDeadObject || val.m_bPooled)
						{
							return;
						}
						list = new List<ConnectionString>(4);
						val.m_bPooled = true;
						list.Add(val);
						base.Put(val.m_key, list);
						flag = true;
					}
				}
			}
			if (flag)
			{
				return;
			}
			bool flag2 = false;
			lock (m_sync)
			{
				if (val.m_bDeadObject || val.m_bPooled)
				{
					return;
				}
				list = Get(val.m_key);
				if (list == null)
				{
					return;
				}
				lock (list)
				{
					for (int i = 0; i < list.Count; i++)
					{
						if (flag2)
						{
							break;
						}
						ConnectionString connectionString = list[i];
						if (connectionString.m_compString == val.m_compString && connectionString.m_osUserName == val.m_osUserName)
						{
							bool flag3 = false;
							bool flag4 = false;
							if (val.m_orclCredential != null && connectionString.m_orclCredential != null)
							{
								flag3 = val.m_orclCredential.Equals(connectionString.m_orclCredential);
							}
							else
							{
								flag4 = ConnectionString.SecureStringsEqual(connectionString.m_securedPassword, val.m_securedPassword) && ConnectionString.SecureStringsEqual(connectionString.m_securedProxyPassword, val.m_securedProxyPassword);
							}
							if (flag3 || flag4)
							{
								flag2 = true;
								break;
							}
						}
					}
					if (!flag2)
					{
						val.m_bPooled = true;
						list.Add(val);
					}
				}
			}
		}
	}
}
